<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once "../config.php";
global $db_addr;

$update_profile_bin = "/home/luanjunyi/yhhd/py/taras/tools/update_profile -h $db_addr ";

if(isset($_GET['ajax'])) {
  if (!$_POST || !isset($_POST['cmd'])) {
    echo 'please send me POST';

  } else {
    chdir('../tmp');
    $cmd =  $update_profile_bin . $_POST['cmd'];
    system($cmd);
  }
  return;
}

?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

   <script type="text/javascript" src="/lib/js/jquery.js"></script>
   <link rel="stylesheet" type="text/css" href="/lib/css/reset.css">
   <link rel="stylesheet" type="text/css" href="css/import_user.css">
   <script type="text/javascript" src="/lib/js/sprintf.js"></script>
   <script type="text/javascript" src="js/import_user.js"></script>

   <title>提交，更新用户信息 | Taras 后台管理</title>
</head>


<?php

require_once "../config.php";
require_once "../util.php";

connect_db();

function is_db_column($col) {
  return $col != "nickname" && $col != "description" && $col != "sina_tags" && $col != "photo";
}


if (isset($_FILES['user-sheet']['tmp_name'])) {
  $user_content = file_get_contents($_FILES['user-sheet']['tmp_name']);
  $user_content = rmBOM($user_content);
  $user_content = str_replace('＃', '#', $user_content);

  $arr_user = array_map("trim", explode("\n", $user_content));

  $result_table = "";

  foreach($arr_user as $key=>$value){
    $value = trim($value);

    // Ignore empty line
    if (strlen($value) == 0)
      continue;

    $cur_row_html = '';
    $cur_row = array_map("trim", explode(',',$value));


    foreach($cur_row as $index=>$item) {
      if ($key > 0 && $index == $col_num - 5) {
        $cur_row_html .= sprintf('<td align="center"><input type="text" class="editable" value="%s"></input></td>' ,$item);
      } else {
        $cur_row_html .= sprintf('<td align="center">%s</td>', $item);
      }
    }

    if ($key == 0) {
      $cur_row_html .= '<td bgcolor="#ff7a7a" width="100">状态</td>';
      $col_num = count($cur_row) + 1;
    } else {
      for ($i = 0; $i < $col_num - count($cur_row); ++$i) {
        $cur_row_html .= '<td></td>';
      }
    }

    $cur_row_html = "<tr>" . $cur_row_html . "</tr>";
    $result_table .= $cur_row_html;
  }

  $result_table = '<table id="user-table" border="1" cellpadding="10">' . $result_table . "</table>";
?>

<body>
<center>
<button id="submit-ajax" style="margin-bottom: 15px; display: block; font-size: large">点我提交下面的东东!</button>
<?php echo $result_table; ?>
</center>
</body>
</html>

<?php
} else {
?>

<body>
  
  <div id="main">
    <center>
      <form action="index.php" method="post" enctype="multipart/form-data">
        <input type="file" name="user-sheet" size="50" id="file-input">
        <input type="submit" value="提交">
      </form>

      <table id="item-table" border="8">
        
      </table>
    </center>
  </div>

  <div id="adhoc">
    <form id="adhoc-form" action="upload_user.php" method="post">
      <label>邮箱:</label> <input type="text" name="email" value=""></input>
      <label>密码:</label> <input name="password" value="" type="text"/>
      <label>categories:</label> <input name="categories" value="" type="text"/>
      <label>tags:</label> <input name="tags" value="" type="text"/>
      <label>poison tags:</label> <input name="poison_tags" value="" type="text"/>
      <label>victim_keywords:</label> <input name="victim_keywords" value="" type="text"/>
      <label>enabled:</label><input type="checkbox" name="enabled" value="yeah" />
      <input type="submit" value="更新" />
    </form>
  </div>

</body>
</html>

<?php }?>
